from nicegui import ui
from typing import cast, Generic, TypeVar

# region 通用定义，应该放在通用库中

_TSlot = TypeVar("_TSlot")


class TemplatePage(Generic[_TSlot]):
    def __init__(self, **kws: ui.element) -> None:
        self._slots = kws

    def replace(self, slot: _TSlot):
        """
        替换某个 slot
        """
        ele = self.append(slot)
        ele.clear()
        return ele

    def append(self, slot: _TSlot):
        """
        保留 base 模板的默认内容，往里面追加新组件
        """
        ele = self._slots[cast(str, slot)]
        return cast(ui.element, ele)


# endregion
